package gui.obrazky;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 * Panel s obrazkem a jeho od/zakryvanaim.
 * @author schovjan
 * @date Jan 10, 2012
 */
public class ObrazekPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private int pocetDiluRozdeleni;
    private Dimension velikost = new Dimension(400, 300);
    private Dimension velikostCernoty;
    private JLabel lObrazek = new JLabel();
    private JPanel pClona = new JPanel();


    /**
     * Konstruktor s parametrem obrazku
     * @param pocetDiluRozdeleni
     */
    public ObrazekPanel(BufferedImage obrazek, int pocetDiluRozdeleni) {
	super();
	this.pocetDiluRozdeleni = pocetDiluRozdeleni;
	int vyskaCernoty = 300 / (pocetDiluRozdeleni / 5);
	velikostCernoty = new Dimension(80, vyskaCernoty);
	setObrazek(obrazek);
	createGUI();
    }


    /**
     * Vytovreni GUI
     */
    private void createGUI() {
	this.setLayout(null);
	this.setSize(velikost);
	this.setMinimumSize(velikost);
	this.setMaximumSize(velikost);
	// clona obrazku
	FlowLayout layout = new FlowLayout();
	layout.setAlignment(FlowLayout.LEADING);
	layout.setHgap(0);
	layout.setVgap(0);
	pClona.setOpaque(false);
	pClona.setSize(velikost);
	pClona.setLayout(layout);
	for (int i = 0; i < pocetDiluRozdeleni; i++) {
	    JPanel pCernota = new JPanel();
	    pCernota.setBackground(Color.BLACK);
	    pCernota.setPreferredSize(velikostCernoty);
	    pClona.add(pCernota);
	}
	pClona.setBounds(0, 0, 400, 300);
	this.add(pClona);
	// obrazek
	lObrazek.setBounds(0, 0, 400, 300);
	this.add(lObrazek);
    }


    /**
     * Nastaveni obrazku
     * @param obrazek
     */
    public void setObrazek(BufferedImage obrazek) {
	BufferedImage upravenyObrazek = new BufferedImage(velikost.width, velikost.height, obrazek.getType());
	Graphics2D g = upravenyObrazek.createGraphics();
	g.drawImage(obrazek, 0, 0, velikost.width, velikost.height, null);
	g.dispose();
	g.setComposite(AlphaComposite.Src);
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	lObrazek.setIcon(new ImageIcon(upravenyObrazek));
    }


    /**
     * Prida jedno cerne pole - vetsi skryti obrazku.
     */
    public void addCernePole() {
	if (pClona.getComponents().length < this.pocetDiluRozdeleni) {
	    JPanel pCernota = new JPanel();
	    pCernota.setBackground(Color.BLACK);
	    pCernota.setPreferredSize(velikostCernoty);
	    pClona.add(pCernota);
	    SwingUtilities.updateComponentTreeUI(ObrazekPanel.this);
	}
    }


    /**
     * Odebere jedno cerne pole - poodkryje obrazek.
     */
    public void removeCernePole() {
	if (pClona.getComponents().length > 0)
	    pClona.remove(pClona.getComponents().length - 1);
	SwingUtilities.updateComponentTreeUI(ObrazekPanel.this);
    }


    /**
     * Vrarti pocetdilu rozdeleni obrazku
     * @return
     */
    public int getPocetDiluRozdeleni() {
	return this.pocetDiluRozdeleni;
    }
    
    public int getPocetDiluZbyvajicich() {
	return pClona.getComponentCount();
    }
}
